package day17.Xml;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.ArrayList;
import java.util.List;

public class Lian02 {
    public static void main(String[] args) throws DocumentException {
        SAXReader saxReader=new SAXReader();
        Document document = saxReader.read("jinjie\\xml\\student.xml");
        Element rootElement = document.getRootElement();
//        获取单个标签
        Element element = rootElement.element("student");
//        获取多个标签
        ArrayList<Student>list=new ArrayList<>();
        List<Element> listelements = rootElement.elements("student");
        for (Element listelement : listelements) {
            Attribute attribute = listelement.attribute("id");
            String id = attribute.getValue();
            Element nameelement = listelement.element("name");
            String name = nameelement.getText();
            Element ageelement = listelement.element("age");
            String age = ageelement.getText();
            Student s=new Student(id,name,Integer.parseInt(age));
            list.add(s);
        }
        for (Student student : list) {
            System.out.println(student);
        }

    }
}
